/*
 * Copyright 2007 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.starsource.forum.client.view;
	
import org.starsource.forum.client.domain.MessageObject;
import org.starsource.forum.client.domain.SessionObject;
import org.starsource.forum.client.handler.MessageUpdateHandler;
import org.starsource.forum.client.handler.SessionUpdateHandler;
import org.starsource.forum.client.web.ForumService;
import org.starsource.forum.client.web.ForumServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.ResizeComposite;
import com.google.gwt.user.client.ui.Widget;

/**
 * A composite for displaying the details of an email message.
 */
public class SessionDetail extends ResizeComposite {
	private final ForumServiceAsync forumService = GWT.create(ForumService.class);

	interface Binder extends UiBinder<Widget, SessionDetail> { }
	private static final Binder binder = GWT.create(Binder.class);

	@UiField Element subject;
	@UiField Element sender;
	@UiField Element date;
	@UiField SessionDetailToolbar toolbar;
	@UiField FlexTable content;

	private MessageUpdateHandler messageUpdateHandler = new MessageUpdateHandler() {

		@Override
		public void onMessageUpdate(MessageObject messageObject) {
			SessionDetail.this.updateData();
		}
		
	};
	
//	private SessionUpdateHandler sessionUpdateHandler;
	public void setSessionUpdateHandler(SessionUpdateHandler sessionUpdateHandler) {
//		this.sessionUpdateHandler = sessionUpdateHandler;
		toolbar.setSessionUpdateHandler(sessionUpdateHandler);
	}
	
	public SessionDetail() {
		initWidget(binder.createAndBindUi(this));
	}

	private SessionObject sessionObject;
	public void setItem(SessionObject item) {
		this.sessionObject = item;
		toolbar.setSessionObject(item);

//		subject.setInnerText("");
//		sender.setInnerText("");
//		date.setInnerText("");

		updateData();
	}
	
	private void updateData() {
		if (sessionObject == null) {
			updateView();
			return;
		}


		forumService.getSessionDetail(sessionObject.getId(), new AsyncCallback<SessionObject>(){
	
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
	
			@Override
			public void onSuccess(SessionObject result) {
				SessionDetail.this.sessionObject = result;
				toolbar.setSessionObject(result);
				updateView();
			}
			
		});
	}

	private void updateView() {
		if (this.sessionObject == null) {
			this.sender.setInnerText("");
			this.subject.setInnerText("");
			this.date.setInnerText("");
			this.content.removeAllRows();
			return;
		}
		
		if (this.sessionObject.getOwner() != null) {
			sender.setInnerText(this.sessionObject.getOwner().getName());
		}
		
		if (this.sessionObject.getTitle() == null || this.sessionObject.getTitle().equals("")) {
			subject.setInnerText("无题");
		} else {
			subject.setInnerText(this.sessionObject.getTitle());
		}
		
		date.setInnerText(this.sessionObject.getUpdateTimeString());

		content.removeAllRows();

		for(int index = 0; index < this.sessionObject.getMessages().size(); index ++) {
			MessageObject messageObject = this.sessionObject.getMessages().get(index);
			MessageDetail messageDetail = new MessageDetail(messageObject, messageUpdateHandler);
			content.setWidget(index, 0, messageDetail);
		}
	}
}
